//
//  AsiaHomePageViewController.m
//  Interface
//
//  Created by Letme on 15-4-14.
//  Copyright (c) 2015年 zhenjiea. All rights reserved.
//

#import "AsiaHomePageViewController.h"

@interface AsiaHomePageViewController ()

@end

@implementation AsiaHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    //欧洲国家6个国家 图片/名字
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/6/"];
    self.navigationController.navigationBar.translucent = NO;
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
      
        
        self.allModelArray = [NSMutableArray array];
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *smalldic in array) {
            HomePageModel *model = [[HomePageModel alloc] init];
            model.name = [smalldic objectForKey:@"name"];
            model.imageUrl = [smalldic objectForKey:@"cover_route_map_cover"];
            [self.allModelArray addObject:model];
            NSLog(@"%@", model.name);
            
            [[DataBaseHandle shareDataBaseHandel]openDB];
            [[DataBaseHandle shareDataBaseHandel]createAsia];
            NSMutableArray *array = [[DataBaseHandle shareDataBaseHandel]selectAllAsiaName];
            
            if (![array containsObject:model.name]) {
                [[DataBaseHandle shareDataBaseHandel] insertAsia:model];
            }
            
            if ([model.name isEqualToString:@"新加坡"] || [model.name isEqualToString:@"印度尼西亚"]) {
                [[DataBaseHandle shareDataBaseHandel]deleteCountryInAsian:model.name];
            }
            
        }
#warning remove
        [self.allModelArray removeObjectAtIndex:2];
        [self.allModelArray removeObjectAtIndex:4];
        
        //NSLog(@"所有欧洲国家一级页面:%@", self.allModelArray);
        //NSLog(@"new:%@", self.allModelArray);
        
        [self.myCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        self.allModelArray = [NSMutableArray array];
        [[DataBaseHandle shareDataBaseHandel]openDB];
        self.allModelArray = [[DataBaseHandle shareDataBaseHandel] selectAllAsiaModel];
        [self.myCollectionView reloadData];
        
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 70) / 2, (self.view.frame.size.width - 70) / 2);
    flowlayout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
#warning 设置头
    //flowlayout.headerReferenceSize = CGSizeMake(0, 50);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 15 + (self.view.frame.size.height) / 15, self.view.frame.size.width, self.view.frame.size.height - 150) collectionViewLayout:flowlayout];
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView release];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
#warning 设置
    [self.myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allModelArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageModel *model = [self.allModelArray objectAtIndex:indexPath.row];
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor lightGrayColor];
    cell.model = model;
    return cell;
}

// 点击国家 进入城市vc
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"东京", @"京都", @"大阪", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/2/6/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/2/3/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url3 = @"http://api.breadtrip.com/destination/place/3/3/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, url3, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    if (indexPath.row == 1) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"首尔", @"济州", @"釜山", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/222/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/7402/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url3 = @"http://api.breadtrip.com/destination/place/3/6975/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, url3, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    if (indexPath.row == 2) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"伊斯坦布尔", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/33611/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    if (indexPath.row == 3) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"迪拜", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/257/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    if (indexPath.row == 4) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"沙巴", @"仙本那", @"兰卡威", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/2/18/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/23/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url3 = @"http://api.breadtrip.com/destination/place/3/9928/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, url3, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        NSLog(@"马来西亚");
        [self.navigationController pushViewController:secondVC animated:YES];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
